import {
  Paragraph,
  TextRun,
  Table,
  TableCell,
  TableRow,
  VerticalAlign,
  TextDirection,
} from "docx";
interface HtmlJson {
  children?: HtmlJson[];
  type?: string;
  name?: string;
  data?: string;
  attribs?: any;
}

export const usehtmlJsonToDocJson = (list: HtmlJson[]) => {
  let data: any[] = [];

  const convertJson = (params: HtmlJson[], bold = false, setData: any[]) => {
    // 返回 TextRun list
    params.map((res) => {
      if (res.type == "tag") {
        if (res.name == "p" && res.children) {
          convertJson(res.children, false, setData);
        } else if (res.name == "strong" && res.children) {
          convertJson(res.children, true, setData);
        } else if (res.name == "span" && res.children) {
          convertJson(res.children, false, setData);
        } else if (res.name == "br" && res.children) {
          setData.push(new Paragraph(" "));
        }
      } else if (res.type == "text") {
        setData.push(
          new TextRun({
            text: res.data,
            bold: bold,
          }),
        );
      }
    });
    return setData;
  };
  function analyze(params: HtmlJson, bold = false, setData: any[]) {
    if (params.type == "tag") {
      if (params.children) {
        setData = setData.concat(convertJson(params.children, false, []));
      }

      //
    } else if (params.type == "text") {
      setData.push(
        new TextRun({
          text: params.data,
          bold: bold,
        }),
      );
    }
    return setData;
  }

  //创建单元内容
  function createTableCell(params: HtmlJson) {
    function createTableCellChildren(params: HtmlJson[]) {
      let cells: any = [];
      params.forEach((res) => {
        if (res.type == "text") {
          cells.push(
            new Paragraph({
              text: res.data,
              style: {},
            }),
          );
        }
      });
      return cells;
    }
    let cellData = [];
    if (params.children) {
      cellData = createTableCellChildren(params.children);
      // debugger
    }
    return new TableCell({
      columnSpan: params.attribs.colspan,
      rowSpan: params.attribs.rowspan,
      children: cellData,
    });
  }
  //返回这个类型 TableRow  tr
  function analyzeTable(params: HtmlJson, bold = false, setData: any[]) {
    let tr: any = params.children?.map((res) => {
      let tableCell = createTableCell(res);
      return tableCell;
    });

    return new TableRow({
      children: tr,
    });
  }
  for (let index = 0; index < list.length; index++) {
    const element: any = list[index];
    // debugger
    if (element.type == "tag") {
      if (element.name == "p" && element.children) {
        data.push(
          new Paragraph({
            children: analyze(element, false, []),
          }),
        );
      } else if (element.name == "strong" && element.children) {
        data.push(
          new Paragraph({
            children: analyze(element, false, []),
          }),
        );
      } else if (element.name == "span" && element.children) {
        data.push(
          new Paragraph({
            children: analyze(element, false, []),
          }),
        );
      } else if (element.name == "table" && element.children) {
        // debugger
        if (element.children) {
          let tbody: HtmlJson = element.children[0];
          if (tbody.children) {
            //TableRow
            let dataTableRow: any = tbody.children.map((res) =>
              analyzeTable(res, false, []),
            );
            data.push(
              new Table({
                rows: dataTableRow,
              }),
            );
          }
        }
      }
    }
  }

  return data;
};
